package com.totsp.conference;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.totsp.conference.data.DataHelper;
import com.totsp.conference.data.Presentation;

import java.util.ArrayList;
import java.util.Comparator;

public class PresentationList extends ConfActivity {

   private ArrayList<Presentation> presentations;

   private ListView listView;
   private ArrayAdapter<Presentation> adapter;
   private TextView title;

   private ProgressDialog progressDialog;

   @Override
   public void onCreate(final Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      setContentView(R.layout.list_layout);

      progressDialog = new ProgressDialog(this);
      progressDialog.setMessage("Retrieving data...");
      progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
      progressDialog.setCancelable(false);

      listView = (ListView) findViewById(R.id.listview);
      listView.setOnItemClickListener(new OnItemClickListener() {
         public void onItemClick(final AdapterView<?> parent, final View v, final int index, final long id) {
            Intent intent = new Intent(PresentationList.this, PresentationDetail.class);
            intent.putExtra(Constants.PRES_NAME, PresentationList.this.presentations.get(index).title);
            intent.putExtra(Constants.PRES_ABSTRACT, PresentationList.this.presentations.get(index).description);
            intent.putExtra(Constants.SPEAKER, PresentationList.this.presentations.get(index).speaker.fName + " "
                     + PresentationList.this.presentations.get(index).speaker.lName);
            // /intent.putExtra(Constants.DATETIME,
            // PresentationList.this.presentations.get(index).datetime);
            PresentationList.this.startActivity(intent);
         }
      });

      presentations = new ArrayList<Presentation>();
      adapter = new PresentationAdapter(this, 0, presentations);
      listView.setAdapter(adapter);

      title = (TextView) findViewById(R.id.listtitle);
      title.setText(R.string.presentations_title);
   }

   @Override
   public void onResume() {
      super.onResume();
      if (!app.getPresentationsCache().isEmpty() && presentations.isEmpty()) {
         presentations.clear();
         presentations.addAll(app.getPresentationsCache());
      } else if (presentations.isEmpty()) {
         if (app.connectionPresent()) {
            new BackgroundTask().execute();
         } else {
            Toast.makeText(this, "Network not available, cannot retrieve data", Toast.LENGTH_SHORT).show();
         }
      }
   }

   @Override
   protected void onPause() {
      if (progressDialog.isShowing()) {
         progressDialog.dismiss();
      }
      super.onPause();
   }

   private class PresentationAdapter extends ArrayAdapter<Presentation> {

      LayoutInflater vi = (LayoutInflater) PresentationList.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      private final ArrayList<Presentation> presentations;

      public PresentationAdapter(final Context context, final int resId, final ArrayList<Presentation> presentations) {
         super(context, resId, presentations);
         this.presentations = presentations;
      }

      @Override
      public View getView(final int position, final View convertView, final ViewGroup parent) {
         // re-use the convertView, try not to recreate objects here or
         // inflate every time (expensive)
         View v = convertView;
         if (v == null) {
            v = vi.inflate(R.layout.listviewitem, null);
         }

         Presentation p = presentations.get(position);
         if (p != null) {
            ((TextView) v.findViewById(R.id.list_item_above)).setText(p.title);
            ((TextView) v.findViewById(R.id.list_item_below)).setText(p.speaker.fName + " " + p.speaker.lName);
         }
         return v;
      }
   }

   private class BackgroundTask extends AsyncTask<Void, Void, ArrayList<Presentation>> {

      @Override
      protected void onPreExecute() {
         if (!progressDialog.isShowing()) {
            progressDialog.show();
         }
      }

      @Override
      protected ArrayList<Presentation> doInBackground(Void... args) {
         return DataHelper.getPresentations();
      }

      @Override
      protected void onPostExecute(ArrayList<Presentation> newPresentations) {
         if (progressDialog.isShowing()) {
            progressDialog.hide();
         }

         if (newPresentations != null && !newPresentations.isEmpty()) {
            presentations.clear();
            presentations.addAll(newPresentations);
            app.setPresentationsCache(newPresentations);
            adapter.notifyDataSetChanged();
         }
      }
   }

   //
   // Sort Comparators
   //
   private class PresNameComparator implements Comparator<Presentation> {
      public int compare(final Presentation p1, final Presentation p2) {
         String name1 = p1.title;
         String name2 = p2.title;
         return name1.toLowerCase().compareTo(name2.toLowerCase());
      }
   }

   private class PresSpeakerNameComparator implements Comparator<Presentation> {
      public int compare(final Presentation p1, final Presentation p2) {
         String name1 = p1.speaker.lName;
         String name2 = p2.speaker.lName;
         return name1.toLowerCase().compareTo(name2.toLowerCase());
      }
   }
}